package com.xuecheng.media.controller;

import com.xuecheng.api.learning.model.vo.ResourceUrlModel;
import com.xuecheng.api.media.MediaApi;
import com.xuecheng.api.media.model.dto.MediaDTO;
import com.xuecheng.api.media.model.qo.QueryMediaModel;
import com.xuecheng.api.media.model.vo.MediaVO;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.domain.response.RestResponse;
import com.xuecheng.media.common.utils.UAASecurityUtil;
import com.xuecheng.media.convert.MediaConvert;
import com.xuecheng.media.service.MediaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 媒资信息 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
public class MediaController implements MediaApi {


    @Autowired
    private MediaService  mediaService;

    @Override
    @PostMapping("media")
    public MediaDTO createMedia(@RequestBody MediaVO mediaVO) {

        Long companyId = UAASecurityUtil.getCompanyId();

        MediaDTO mediaDTO = MediaConvert.INSTANCE.vo2dto(mediaVO);
        mediaDTO.setCompanyId(companyId);

        MediaDTO result = mediaService.createMedia(mediaDTO);

        return result;
    }

    @Override
    @PostMapping("media/list")
    public PageVO queryMediaList(PageRequestParams params, @RequestBody QueryMediaModel model) {

        Long companyId = UAASecurityUtil.getCompanyId();

        PageVO pageVO = mediaService.queryMediaList(params, model, companyId);

        return pageVO;

    }

    @Override
    @DeleteMapping("media/{mediaId}")
    public void removeMedia(@PathVariable Long mediaId) {
        Long companyId = UAASecurityUtil.getCompanyId();

        mediaService.removeMedia(mediaId, companyId);
    }


    @Override
    @GetMapping("l/media/{mediaId}")
    public RestResponse<MediaDTO> getMediaById4s(@PathVariable Long mediaId) {
        return mediaService.getMediaById4s(mediaId);
    }


    @GetMapping("l/media-url/{mediaId}")
    public RestResponse<ResourceUrlModel> getMediaUrl(@PathVariable Long mediaId) {
        RestResponse<ResourceUrlModel> response = mediaService.getMediaUrl(mediaId);
        return response;
    }


}
